import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/entity/match/match_technic_entity.dart';
import 'package:ball_lion/entity/match/zq_match_info_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/match_detail/soccer/zq_nodata_widget.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class ZqStatisticsWidget extends StatelessWidget {
  const ZqStatisticsWidget({super.key, this.tech, this.info});

  final MatchTechEntity? tech;
  final ZqMatchInfoEntity? info;

  bool isTechEmpty() {
    if (tech?.technicDetail?.isEmpty ?? true) {
      return true;
    }
    var isEmpty = true;
    outloop:
    for (var i = 0; i < tech!.technicDetail!.length; i++) {
      final details = tech!.technicDetail![i];

      for (var j = 0; j < (details.technicDetail?.length ?? 0); j++) {
        final detail = details.technicDetail![j];
        // detail.homeData?.isEmpty
        final hd = int.tryParse(detail.homeData?.replaceAll("%", "") ?? "");
        final gd = int.tryParse(detail.guestData?.replaceAll("%", "") ?? "");
        if (hd != null && gd != null) {
          isEmpty = false;
          break outloop;
        }
      }
    }
    return isEmpty;
  }

  @override
  Widget build(BuildContext context) {
    if (info?.state == MatchState.notStart || isTechEmpty()) {
      return ZqNodataWidget(info: info);
    }
    final List<Widget> list = [SizedBox(height: 20)];
    final len = tech?.technicDetail?.length ?? 0;
    for (var i = 0; i < len; i++) {
      final json = tech!.technicDetail![i];
      if (json.isNoData) {
        continue;
      }
      list.add(_cardBox(Column(
        children: [
          _cardTitle(json.title ?? ""),
          ...json.technicDetail?.filter((item) => item.isHasData).map((e1) =>
                  _middleTitleInt(e1.technicCn ?? "", e1.homeData ?? "",
                          e1.guestData ?? "")
                      .marginOnly(bottom: 8)) ??
              []
        ],
      )));
      if (i != len - 1) {
        list.add(SizedBox(height: 10));
      }
    }

    return Column(children: list);
  }

  Widget _cardBox(Widget child) {
    return Container(
        width: double.infinity,
        padding: EdgeInsets.symmetric(horizontal: 12, vertical: 0),
        margin: EdgeInsets.only(top: 0),
        decoration: BoxDecoration(color: Colors.white),
        child: child);
  }

  Widget _cardTitle(String title) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          // SizedBox(height: 10),
          QsText(title, fontWeight: FontWeight.w500),
          const SizedBox(height: 10),
        ],
      ),
    );
  }

  Widget _middleTitleInt(String title, String leftStr, String rightStr) {
    int left = int.tryParse(leftStr.replaceAll("%", "")) ?? 0;
    int right = int.tryParse(rightStr.replaceAll("%", "")) ?? 0;
    final same = (left == right);
    final total = (left + right) == 0 ? 1 : (left + right);
    final leftWin = (same || (left > right));
    final rightWin = (same || (right > left));
    final leftColor = leftWin ? Colours.homeColorRed : Colours.greyD7;
    final rightColor = rightWin ? Colours.blue2766D6 : Colours.greyD7;
    final leftProgress = left / total;
    final rightProgress = right / total;
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 5),
      height: 42,
      child: Column(
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                width: 80,
                child: QsText(leftStr,
                    fontFamily: "din",
                    fontSize: 16,
                    color: leftWin ? Colours.text : Colours.grey99),
              ),
              const Spacer(),
              QsText(title, fontSize: 12),
              const Spacer(),
              SizedBox(
                width: 80,
                child: QsText(rightStr,
                    textAlign: TextAlign.right,
                    fontSize: 16,
                    fontFamily: "din",
                    color: rightWin ? Colours.text : Colours.grey99),
              ),
            ],
          ),
          const Spacer(),
          Row(children: [
            Expanded(
              child: Transform.rotate(
                  angle: math.pi, child: progress(leftProgress, leftColor)),
            ),
            Expanded(child: progress(rightProgress, rightColor))
          ]),
        ],
      ),
    );
  }

  Widget progress(double percent, Color color) {
    final radius = BorderRadius.horizontal(right: Radius.circular(2));
    return Container(
      height: 6,
      decoration: BoxDecoration(color: Colours.greyF7, borderRadius: radius),
      child: LayoutBuilder(builder: (p0, p1) {
        return Row(
          children: [
            Container(
              width: p1.maxWidth * percent,
              decoration: BoxDecoration(borderRadius: radius, color: color),
            ),
          ],
        );
      }),
    );
  }
}
